package com.wish.banking.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.banking.entity.CfContract;
import com.wish.banking.entity.JrProduct;
import com.wish.banking.entity.JrRentInstallment;
import com.wish.banking.entity.vo.CfContractVO;
import com.wish.banking.service.ICfContractService;
import com.wish.banking.service.IJrProductService;
import com.wish.banking.service.IJrRentInstallmentService;
import com.wish.common.core.usconfig.common.MsgResult;
import com.wish.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 房租分期申请 前端控制器
 * </p>
 *
 * @author cjl
 * @since 2022-04-03
 */
@RestController
@RequestMapping("/rentInstallment")
public class JrRentInstallmentController {

    @Autowired
    private IJrProductService productService;
    @Autowired
    private ICfContractService cfContractService;
    @Autowired
    private IJrRentInstallmentService rentInstallmentService;

    //金融产品集合
    @GetMapping("productList")
    public MsgResult productList(){
        QueryWrapper<JrProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flg",0);
        queryWrapper.eq("jr_product_type",1);
        return MsgResult.succeed(productService.list(queryWrapper));
    }

    //租房合同查询
    @GetMapping("searchCfContract")
    public MsgResult searchCfContract(CfContractVO cfContract){
        return MsgResult.succeed(cfContractService.searchCfContract(cfContract));
    }

    //计算借款金额
    @GetMapping("getLoanAmount")
    public MsgResult getLoanAmount(Integer cfContractId){
        CfContract cfContract = cfContractService.getById(cfContractId);
        //借款金额
        //月租金
        BigDecimal monthRent = cfContract.getMonthRent();
        //n个月
        long months = DateUtil.betweenMonth(cfContract.getStartDate(), cfContract.getEndDate(), true);
        BigDecimal multiply = monthRent.multiply(new BigDecimal(months));
        return MsgResult.succeed(multiply);
    }


    //保存租房分期申请
    @PostMapping("saveRentInstallment")
    public MsgResult saveRentInstallment(@RequestBody JrRentInstallment rentInstallment){

        //创建人及时间
        rentInstallment.setCreateUserid(SecurityUtils.getUserId());
        rentInstallment.setUpdateUserid(SecurityUtils.getUserId());
        rentInstallment.setCreateTime(LocalDateTime.now());
        rentInstallment.setUpdateTime(LocalDateTime.now());
        Snowflake snowflake = IdUtil.getSnowflake();
        long l = snowflake.nextId();
        rentInstallment.setRentInstallmentOrderNo("FZFQ"+l);

        //获取出房合同
        CfContract cfContract = cfContractService.getById(rentInstallment.getCfContractId());

        //借款金额
        //月租金
        BigDecimal monthRent = cfContract.getMonthRent();
        //n个月
        long months = DateUtil.betweenMonth(cfContract.getStartDate(), cfContract.getEndDate(), true);
        BigDecimal multiply = monthRent.multiply(new BigDecimal(months));
        rentInstallment.setLoanAmount(multiply);

        //应收租金合计
        BigDecimal multiply1 = multiply.multiply(new BigDecimal(0.2)).setScale(2,BigDecimal.ROUND_HALF_UP);
        rentInstallment.setReLeaseCount(multiply1.toString());
        System.err.println(multiply1.toString());

        //公寓分期利息合计
        BigDecimal multiply3 = monthRent.multiply(rentInstallment.getBusinessRate()).multiply(new BigDecimal(months)).setScale(2,BigDecimal.ROUND_HALF_UP);
        rentInstallment.setInInterestCount(multiply3.toString());

        //服务费
        BigDecimal multiply2 = multiply1.multiply(rentInstallment.getServiceFee()).multiply(new BigDecimal((0.02))).setScale(2,BigDecimal.ROUND_HALF_UP);
        rentInstallment.setSerCharge(multiply2);

        //申请状态(申请中)
        rentInstallment.setApplyState(1);

        rentInstallmentService.save(rentInstallment);
        return MsgResult.succeed();
    }
}
